package me.landery.modules.sinco.rest;


import cn.hutool.core.date.DateUtil;
import com.alibaba.fastjson.JSONObject;
import io.lettuce.core.dynamic.annotation.Param;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import me.landery.annotation.Log;
import me.landery.exception.BadRequestException;
import me.landery.modules.sinco.domain.Call;
import me.landery.modules.sinco.repository.ModeRepository;
import me.landery.modules.sinco.service.CallService;
import me.landery.modules.sinco.service.dto.CallQueryCriteria;
import me.landery.modules.system.service.UserService;
import me.landery.modules.system.service.dto.UserDto;
import me.landery.utils.SecurityUtils;
import me.landery.utils.StringUtils;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;

/**
 * @Author cc.w
 * @Date 2022/6/7 21:36
 * @Version 1.0
 */
@RestController
@RequiredArgsConstructor
@Api(tags = "Sinco：调用记录管理")
@RequestMapping("/sinco/call")
public class CallController {
    private final CallService callService;
    private static final String ENTITY_NAME = "mode";



    @ApiOperation("查询调用记录")
    @GetMapping
    @PreAuthorize("@el.check('call:list','admin')")
    public ResponseEntity<Object> queryMode(CallQueryCriteria criteria, Pageable pageable){
        return new ResponseEntity<>(callService.queryAll(criteria, pageable), HttpStatus.OK);
    }

    @ApiOperation("查询调用记录")
    @GetMapping("/web")
    @PreAuthorize("@el.check('call:weblist','admin')")
    public ResponseEntity<Object> queryModeWeb(CallQueryCriteria criteria, Pageable pageable){
        Pageable pageable1 = PageRequest.of(pageable.getPageNumber()-1,pageable.getPageSize(),pageable.getSort());
        return new ResponseEntity<>(callService.queryAll(criteria, pageable1), HttpStatus.OK);
    }

    @Log("新增调用记录")
    @ApiOperation("新增调用记录")
    @PostMapping
    @PreAuthorize("@el.check('call:add','admin')")
    public ResponseEntity<Object> createCall(@Validated @RequestBody Call resources){
        if (resources.getId() != null) {
            throw new BadRequestException("A new "+ ENTITY_NAME +" cannot already have an ID");
        }
        callService.create(resources);
        return new ResponseEntity<>(HttpStatus.CREATED);
    }

    @Log("获取当天统计结果")
    @ApiOperation("获取当天统计结果")
    @GetMapping("/currentDayTotal")
    @PreAuthorize("@el.check('call:currentDayTotal','admin')")
    public ResponseEntity<Object> queryCurrentDayTotal(@RequestParam(value = "searchDate",defaultValue = "") String dateStr,
                                                       @RequestParam(value = "appId",defaultValue = "")String appId)  {
        Date date;
        if (StringUtils.isBlank(dateStr)){
            date = new Date();
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
               date =  simpleDateFormat.parse(dateStr);
            }catch ( ParseException e){
                throw  new BadRequestException("时间参数错误！");
            }

        }
        if (StringUtils.isBlank(appId)){
            appId = null;
        }
        JSONObject jsonObject = callService.queryCurrentDayTotal(date,appId);

        return new ResponseEntity<>(jsonObject,HttpStatus.OK);
    }

    @Log("近10天统计结果")
    @ApiOperation("近10天统计结果")
    @GetMapping("/his10DayTotal")
    @PreAuthorize("@el.check('call:his10DayTotal','admin')")
    public ResponseEntity<Object> queryHis10DayTotal(@RequestParam(value = "searchDate",defaultValue = "") String dateStr,
                                                     @RequestParam(value = "appId",defaultValue = "")String appId)  {
        Date date;
        if (StringUtils.isBlank(dateStr)){
            date = new Date();
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date =  simpleDateFormat.parse(dateStr);
            }catch ( ParseException e){
                throw  new BadRequestException("时间参数错误！");
            }

        }
        if (StringUtils.isBlank(appId)){
            appId = null;
        }
        JSONObject jsonObject = callService.queryHis10DayTotal(date,appId);

        return new ResponseEntity<>(jsonObject,HttpStatus.OK);
    }
    @Log("获取首页累计调用率")
    @ApiOperation("获取首页累计调用率")
    @GetMapping("/findRecordGk")
    @PreAuthorize("@el.check('call:findRecordGk','admin')")
    public ResponseEntity<Object> findRecordGk()  {

        JSONObject jsonObject = callService.findRecordGk();

        return new ResponseEntity<>(jsonObject,HttpStatus.OK);
    }

    //获取调用占比记录
    @Log("获取调用类型占用统计")
    @ApiOperation("获取调用类型占用统计")
    @GetMapping("/getCallTypePercent")
    @PreAuthorize("@el.check('call:getCallTypePercent','admin')")
    public ResponseEntity<Object> getCallTypePercent(@RequestParam(value = "searchDate",defaultValue = "") String dateStr,
                                                     @RequestParam(value = "appId",defaultValue = "")String appId)  {
        Date date;
        if (StringUtils.isBlank(dateStr)){
            date = new Date();
        }else{
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            try {
                date =  simpleDateFormat.parse(dateStr);
            }catch ( ParseException e){
                throw  new BadRequestException("时间参数错误！");
            }

        }
        if (StringUtils.isBlank(appId)){
            appId = null;
        }
        JSONObject jsonObject = callService.getCallTypePercent(date,appId);

        return new ResponseEntity<>(jsonObject,HttpStatus.OK);
    }
}
